package com.next.muscu.visual;
import com.next.muscu.PersistanceUnitTools;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTree;


import com.next.muscu.data.ExoTreeModel;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.persistence.EntityManager;

public class VisualExoTreePanel extends JPanel 
implements ActionListener{


	
	    JTree tree;
	    private static String SHOW_ANCESTOR_CMD = "showAncestor";

	    public VisualExoTreePanel() {
	        super(new BorderLayout());
	        
	        //Construct the panel with the toggle buttons.
	        JRadioButton showDescendant = 
	                new JRadioButton("A->Z", true);
	        final JRadioButton showAncestor = 
	                new JRadioButton("Z->A");
	        ButtonGroup bGroup = new ButtonGroup();
	        bGroup.add(showDescendant);
	        bGroup.add(showAncestor);
	        showDescendant.addActionListener(this);
	        showAncestor.addActionListener(this);
	        showAncestor.setActionCommand(SHOW_ANCESTOR_CMD);
	        JPanel buttonPanel = new JPanel();
	        buttonPanel.add(showDescendant);
	        buttonPanel.add(showAncestor);

	        //Construct the tree.
	        EntityManager em = PersistanceUnitTools.getOneEntityManager();
		List l = em.createQuery("from ExoTypeExo").getResultList();
	        tree = new JTree(new ExoTreeModel(l) );
	        tree.setCellRenderer(new ExoTypeExoTreeCellRender());
	        JScrollPane scrollPane = new JScrollPane(tree);
	        scrollPane.setPreferredSize(new Dimension(200, 200));

	        //Add everything to this panel.
	        add(buttonPanel, BorderLayout.PAGE_START);
	        add(scrollPane, BorderLayout.CENTER);
	    }

	    /** 
	     * Required by the ActionListener interface.
	     * Handle events on the showDescendant and
	     * showAncestore buttons. 
	     */
	    public void actionPerformed(ActionEvent ae) {
	       /* if (ae.getActionCommand() == SHOW_ANCESTOR_CMD) {
	            tree.showAncestor(true);
	        } else {
	            tree.showAncestor(false);
	        }*/
	    }
	    
	    private static void createAndShowGUI() {
	        //Create and set up the window.
	        JFrame frame = new JFrame("GenealogyExample");
	        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	        //Create and set up the content pane.
	        VisualExoTreePanel newContentPane = new VisualExoTreePanel();
	        newContentPane.setOpaque(true); //content panes must be opaque
	        frame.setContentPane(newContentPane);

	        //Display the window.
	        frame.pack();
	        frame.setVisible(true);
	    }
	    JTree getTree()
	    {
	    	return tree;
	    	
	    }
	    public static void main(String[] args) {
	        //Schedule a job for the event-dispatching thread:
	        //creating and showing this application's GUI.
	        javax.swing.SwingUtilities.invokeLater(new Runnable() {
	            public void run() {
	                createAndShowGUI();
	            }
	        });
	    }
	}
